package List;

import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-02-26
 * Time:19:26
 * Text：相邻两个结点交换
 */

public class Text2 {
    /*
    * 方法一：递归
    * */

    public ListNode swapPairs(ListNode head) {
        if(head==null || head.next==null)return head;
        ListNode t1=swapPairs(head.next.next);

        ListNode t2=head.next;
        t2.next=head;
        head.next=t1;

        return t2;
    }
    /*
    * 循环和迭代
    * */
    public ListNode swapPairs2(ListNode head) {
        ListNode t1=new ListNode();
        ListNode t3=t1;
        t1.next=head;
        while(t1.next!=null && t1.next.next!=null){
            ListNode t2=t1.next;
            ListNode t4=t1.next.next;
            t2.next=t4.next;
            t4.next=t2;
            t1.next=t4;
            t1=t2;
        }
        return t3.next;
    }

    public static void main(String[] args) {

    }
}





